﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class TypeRefDAO//:BaseDAO<RELAXEntities,RelaxType>
   {
       #region value
       private RelaxDataContext _context = new RelaxDataContext();
       #endregion

       #region Hàm lấy dữ liệu

       public IQueryable<RelaxTypeDataContract> GetTypeDataContracts()
       {
           var lReturn = from t in _context.TYPE_REFs
               join v in _context.TYPE_VALUEs on t.id equals v.typeid
               select new RelaxTypeDataContract
               {
                   Id = Convert.ToInt32(t.id),
                   Name = v.name,
                   Tag = v.tag,
                   Active = Convert.ToInt32(t.active),
                   Ord = Convert.ToInt32(t.ord),
                   Decription = v.decription,
                   Languagecode = Convert.ToInt32(v.languagecode)
               };
           return lReturn;
       }
       public IQueryable<RelaxTypeDataContract> GetTypeDataContract()
       {
           var lReturn = from t in _context.TYPE_REFs
                         select new RelaxTypeDataContract
                         {
                             Id = Convert.ToInt32(t.id),
                             Active = Convert.ToInt32(t.active),
                             Ord = Convert.ToInt32(t.ord)
                         };
           return lReturn;
       }
       #endregion
       #region Add
       public void Add(TYPE_REF type)
       {
           _context.TYPE_REFs.InsertOnSubmit(type);
           _context.SubmitChanges();

       }
       #endregion
       #region Update

       public void Update(TYPE_REF type)
       {
           var first = _context.TYPE_REFs.FirstOrDefault(p => p.id == type.id);
           if (first != null)
           {
               first.ord = type.ord;
               first.active = type.active;
               _context.SubmitChanges();
           }
       }
       #endregion

       #region Delete
       public void Delete(long iId)
       {
           var first = _context.TYPE_REFs.FirstOrDefault(p => p.id == iId);
           if (first != null)
           {
               _context.TYPE_REFs.DeleteOnSubmit(first);
               _context.SubmitChanges();
           }
       }
       #endregion
   }
}
